//
//  SPCardModelDemo.m
//  ShandongPoker
//
//  Created by Developer on 2025-01-08.
//  Copyright © 2025 ShandongPoker. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SPCard.h"
#import "SPDeck.h"
#import "SPHandEvaluator.h"
#import "SPPlayer.h"

/**
 * 扑克牌数据模型演示程序
 * 用于验证第一阶段实现的功能
 */
void demonstrateCardModel(void) {
    NSLog(@"=== 山东德州扑克 - 扑克牌数据模型演示 ===");
    
    // 1. 测试扑克牌创建和基本属性
    NSLog(@"\n1. 测试扑克牌创建和基本属性:");
    SPCard *aceOfHearts = [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankAce];
    SPCard *kingOfSpades = [[SPCard alloc] initWithSuit:SPCardSuitSpades rank:SPCardRankKing];
    
    NSLog(@"红桃A: %@ (值: %ld, 红色: %@)", aceOfHearts.displayName, (long)aceOfHearts.value, aceOfHearts.isRed ? @"是" : @"否");
    NSLog(@"黑桃K: %@ (值: %ld, 黑色: %@)", kingOfSpades.displayName, (long)kingOfSpades.value, kingOfSpades.isBlack ? @"是" : @"否");
    
    // 2. 测试牌组创建和洗牌
    NSLog(@"\n2. 测试牌组创建和洗牌:");
    SPDeck *deck = [[SPDeck alloc] init];
    NSLog(@"创建牌组: %ld张牌", (long)deck.allCards.count);
    
    [deck shuffle];
    NSLog(@"洗牌后: %@", deck.isShuffled ? @"已洗牌" : @"未洗牌");
    
    // 3. 测试发牌功能
    NSLog(@"\n3. 测试发牌功能:");
    NSArray<SPCard *> *dealtCards = [deck dealCards:5];
    NSLog(@"发出5张牌:");
    for (SPCard *card in dealtCards) {
        NSLog(@"  %@", card.displayName);
    }
    NSLog(@"剩余牌数: %ld", (long)deck.remainingCards);
    
    // 4. 测试玩家模型
    NSLog(@"\n4. 测试玩家模型:");
    SPPlayer *humanPlayer = [[SPPlayer alloc] initWithName:@"玩家" chips:1000];
    SPPlayer *aiPlayer = [[SPPlayer alloc] initWithName:@"AI对手" chips:1000 aiDifficulty:SPAIDifficultyMedium];
    
    NSLog(@"人类玩家: %@ (筹码: %ld, AI: %@)", humanPlayer.name, (long)humanPlayer.chips, humanPlayer.isAI ? @"是" : @"否");
    NSLog(@"AI玩家: %@ (筹码: %ld, AI: %@, 难度: %ld)", aiPlayer.name, (long)aiPlayer.chips, aiPlayer.isAI ? @"是" : @"否", (long)aiPlayer.aiDifficulty);
    
    // 5. 测试玩家下注
    NSLog(@"\n5. 测试玩家下注:");
    [humanPlayer bet:100];
    [aiPlayer bet:200];
    NSLog(@"玩家下注后: 筹码 %ld, 当前下注 %ld", (long)humanPlayer.chips, (long)humanPlayer.currentBet);
    NSLog(@"AI下注后: 筹码 %ld, 当前下注 %ld", (long)aiPlayer.chips, (long)aiPlayer.currentBet);
    
    // 6. 测试牌型判断
    NSLog(@"\n6. 测试牌型判断:");
    
    // 创建皇家同花顺
    NSArray<SPCard *> *royalFlush = @[
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankKing],
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankQueen],
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankJack],
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankTen]
    ];
    
    SPHandResult *royalFlushResult = [SPHandEvaluator evaluateFiveCardHand:royalFlush];
    NSLog(@"皇家同花顺: %@ (分数: %ld)", royalFlushResult.handName, (long)royalFlushResult.score);
    
    // 创建四条
    NSArray<SPCard *> *fourOfAKind = @[
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitSpades rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitDiamonds rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitClubs rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankKing]
    ];
    
    SPHandResult *fourOfAKindResult = [SPHandEvaluator evaluateFiveCardHand:fourOfAKind];
    NSLog(@"四条: %@ (分数: %ld)", fourOfAKindResult.handName, (long)fourOfAKindResult.score);
    
    // 创建一对
    NSArray<SPCard *> *onePair = @[
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitSpades rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitDiamonds rank:SPCardRankKing],
        [[SPCard alloc] initWithSuit:SPCardSuitClubs rank:SPCardRankQueen],
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankJack]
    ];
    
    SPHandResult *onePairResult = [SPHandEvaluator evaluateFiveCardHand:onePair];
    NSLog(@"一对: %@ (分数: %ld)", onePairResult.handName, (long)onePairResult.score);
    
    // 7. 测试牌型比较
    NSLog(@"\n7. 测试牌型比较:");
    NSComparisonResult comparison1 = [SPHandEvaluator compareHandResult:royalFlushResult withResult:fourOfAKindResult];
    NSComparisonResult comparison2 = [SPHandEvaluator compareHandResult:fourOfAKindResult withResult:onePairResult];
    NSComparisonResult comparison3 = [SPHandEvaluator compareHandResult:onePairResult withResult:royalFlushResult];
    
    NSLog(@"皇家同花顺 vs 四条: %@", comparison1 == NSOrderedDescending ? @"皇家同花顺胜" : @"四条胜");
    NSLog(@"四条 vs 一对: %@", comparison2 == NSOrderedDescending ? @"四条胜" : @"一对胜");
    NSLog(@"一对 vs 皇家同花顺: %@", comparison3 == NSOrderedDescending ? @"一对胜" : @"皇家同花顺胜");
    
    // 8. 测试德州扑克手牌评估
    NSLog(@"\n8. 测试德州扑克手牌评估:");
    NSArray<SPCard *> *holeCards = @[
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitSpades rank:SPCardRankAce]
    ];
    
    NSArray<SPCard *> *communityCards = @[
        [[SPCard alloc] initWithSuit:SPCardSuitDiamonds rank:SPCardRankAce],
        [[SPCard alloc] initWithSuit:SPCardSuitClubs rank:SPCardRankKing],
        [[SPCard alloc] initWithSuit:SPCardSuitHearts rank:SPCardRankQueen]
    ];
    
    SPHandResult *bestHand = [SPHandEvaluator evaluateHandWithHoleCards:holeCards communityCards:communityCards];
    NSLog(@"手牌: %@, %@", holeCards[0].displayName, holeCards[1].displayName);
    NSLog(@"公共牌: %@, %@, %@", communityCards[0].displayName, communityCards[1].displayName, communityCards[2].displayName);
    NSLog(@"最佳牌型: %@ (分数: %ld)", bestHand.handName, (long)bestHand.score);
    
    NSLog(@"\n=== 演示完成 ===");
}

// 如果需要在其他地方调用演示，可以导出这个函数
void runCardModelDemo(void) {
    NSLog(@"🎯 ===== 扑克牌数据模型演示程序开始 =====");
    NSLog(@"🚀 演示程序正在运行...");
    
    // 创建一副扑克牌
    NSLog(@"\n📦 1. 创建一副扑克牌");
    SPDeck *deck = [[SPDeck alloc] init];
    NSLog(@"✅ 扑克牌创建成功，共 %lu 张牌", (unsigned long)deck.allCards.count);
    
    // 洗牌
    NSLog(@"\n🔄 2. 洗牌");
    [deck shuffle];
    NSLog(@"✅ 洗牌完成");
    
    // 发牌演示
    NSLog(@"\n🎴 3. 发牌演示");
    NSArray<SPCard *> *playerHand = [deck dealCards:2];
    NSArray<SPCard *> *communityCards = [deck dealCards:5];
    
    NSLog(@"👤 玩家手牌:");
    for (SPCard *card in playerHand) {
        NSLog(@"   %@", [card description]);
    }
    
    NSLog(@"🌐 公共牌:");
    for (SPCard *card in communityCards) {
        NSLog(@"   %@", [card description]);
    }
    
    // 牌型判断演示
    NSLog(@"\n🔍 4. 牌型判断演示");
    NSArray<SPCard *> *allCards = [playerHand arrayByAddingObjectsFromArray:communityCards];
    SPHandResult *result = [SPHandEvaluator evaluateFiveCardHand:allCards];
    
    NSLog(@"🎯 最佳牌型: %@", result.handName);
    NSLog(@"🏆 牌型等级: %ld", (long)result.handRank);
    NSLog(@"💎 牌型分数: %ld", (long)result.score);
    
    // 玩家管理演示
    NSLog(@"\n👥 5. 玩家管理演示");
    SPPlayer *player = [[SPPlayer alloc] initWithName:@"玩家1" chips:1000];
    
    // 添加手牌
    for (SPCard *card in playerHand) {
        [player addHoleCard:card];
    }
    
    NSLog(@"👤 玩家信息:");
    NSLog(@"   姓名: %@", player.name);
    NSLog(@"   筹码: ¥%ld", (long)player.chips);
    NSLog(@"   手牌数量: %lu", (unsigned long)player.holeCards.count);
    NSLog(@"   是否弃牌: %@", player.hasFolded ? @"是" : @"否");
    
    NSLog(@"\n🎉 ===== 演示程序完成 =====");
} 